The Line Edits: the actual position, actual velocity, and command velocity

The Line Edits are used to display the actual position and actual velocity, and to get the command velocity from the user input. The names of these Edits are leActPosition, leActVelocity, and leCommVelocity.

We use the variable pos and vel to get the actual position and velocity. Their initial values are set to zero. The actual position and velocity are gotten through updateAxisData, which is the slot connected to the signal sendData. In The Connect button, we have talked about updateAxisData in the Timer section. When sendData is emitted, updateAxisData is called. It updates leActPosition and leActVelocity using setText, in which we use number to get the actual position and velocity, set the numeric format to f (decimal number without scientific notation), and set the decimal place to three.

The following code is in QtGui.cpp:

QObject::connect(ks, &ksWorker::sendData, this, &QtGui::updateAxisData);

The following code is in ksworker.cpp:

QObject::connect(dataTimer, &QTimer::timeout, [this]() 
{
   if (maSts.State == ecatOP && currentIndex >= 0)
   { 
      double pos = 0.0;
      double vel = 0.0;

      data.powerStatus = powerStatus[currentIndex];
      nRet = GetAxisPosition(currentIndex, McSource::mcActualValue, &pos);
      if (nRet == KsError::errNoError)
         data.actualPos = pos;
      else
         data.actualPos = (double)nRet;

      nRet = GetAxisVelocity(currentIndex, McSource::mcActualValue, &(vel));
      if (nRet == KsError::errNoError)
         data.actualVel = vel;
      else
         data.actualVel = (double)nRet;
   }

   emit sendData(data);
});

The following code is in QtGui.cpp:

void QtGui::updateAxisData(axisData data)
   ...........
   ui->leActPosition->setText(QString::number(data.actualPos, 'f', 3));
   ui->leActVelocity->setText(QString::number(data.actualVel, 'f', 3));
}

For leCommVelocity, we declare the variable commandVelocity and set it to 360 (ksworker.h and ksworker.cpp). Next, we connect the signal textChanged to the slot commandVelocityChanged (QtGui.cpp). When we change the text in leCommVelocity, the program runs commandVelocityChanged, in which commandVelocity is set to the text we entered, and converted to an integer.

The following code is in ksworker.cpp:

ksWorker::ksWorker(QObject *parent)
: QObject(parent), dataTimer(new QTimer(this)), data({false, 0.0, 0.0}),
maSts({ ecatOffline, ecatOffline, 0, 0, 0, { ecatOffline },
{ ecatOffline }, { axisOffline } }), wkState(disconnected),
currentIndex(-1), commandVelocity(360)

The following code is in QtGui.cpp:

QObject::connect(ui->leCommVelocity, &QLineEdit::textChanged, ks, &ksWorker::commandVelocityChanged);

The following code is in ksworker.cpp:

void ksWorker::commandVelocityChanged(const QString &text)
{
   if (text.toInt() >= 0)
      commandVelocity = text.toInt();
}